<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item v-for="(item, index) in breadList" :key="index"
          :to="{ path: item.path }">{{ item.meta.title }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" status-icon>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="ruleForm.userName" />
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" />
          </el-form-item>
          <el-form-item label="用户类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio :label="0">管理员</el-radio>
              <el-radio :label="1">普通用户</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="性别" prop="type">
            <el-radio-group v-model="ruleForm.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="电话" prop="tel">
            <el-input v-model="ruleForm.tel" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email" />
          </el-form-item>

          <el-form-item label="用户状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="0">停用</el-radio>
              <el-radio :label="1">正常</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitFormAdd(ruleFormRef)">新增</el-button>
        <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { useRouter,useRoute } from 'vue-router'
import { ref, reactive, watch } from "vue";
import request from "../../utils/request";
import { ElNotification } from "element-plus";
export default {
  setup() {
    const router = useRouter()
    const route = useRoute()
    let breadList = [] // 路由集合
    const getBreadcrumb = (url) => {
      let matched = url.matched;
      //如果不是首页
      if ("Home" == matched[0].name) {
        matched[0].meta = { title: "系统首页" }
      }
      breadList = matched;
    }
    watch(route, (newValue, oldValue) => {
      // console.log(222222,newValue)
      getBreadcrumb(newValue)
    }, { deep: true, immediate: true });

    const resetForm = (formEl) => {
      if (!formEl) return;
      formEl.resetFields();
    };

    const ruleForm = reactive({
      userName: "",
      password: "",
      type: 0,
      sex: 1,
      address: "",
      phone: "",
      status: 1,
    });
    const rules = reactive({
      username: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur"
        },
        {
          min: 1,
          max: 7,
          message: "长度大于1小于7",
          trigger: "blur",
        },
      ],
      tel: [
        {
          required: true,
          message: "请输入电话号码",
          trigger: "blur",
        },
      ],
      email: [
        {
          required: true,
          message: "请输入邮箱",
          trigger: "blur"
        },
      ],
      type: [
        {
          required: true,
          message: "请选择用户类型",
          trigger: "change",
        },
      ],
      status: [
        {
          required: true,
          message: "请选择用户状态",
          trigger: "change",
        },
      ],
    });
    const ruleFormRef = ref();
    const centerDialogVisible = ref(false);
    //增加用户
    const submitFormAdd = () => {
      request
        .request({
          url: "/user/add",
          data: {
                userName: ruleForm.userName,
                password: ruleForm.password,
                type: ruleForm.type,
                tel: ruleForm.tel,
                sex: ruleForm.sex,
                email: ruleForm.email,
                status: ruleForm.status,
              },
          method: "post",
        }).then((res) => {
          if (res.data.code == 200) {
                ElNotification({
                  title: "添加成功",
                  message: "成功添加用户信息",
                });
                router.push('userInfo')
              } else {
                ElNotification({
                  title: "添加失败123",
                  message: "请联系管理员检查失败原因",
                });
              }
        })
    };

    return {
      breadList,
      resetForm,
      ruleFormRef,
      ruleForm,
      rules,
      centerDialogVisible,
      submitFormAdd
    };
  },
};
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
